Eye_tracking 1
 
Cargando...
Buscando...
Nada coincide
pygaze.py
Ir a la documentación de este archivo.
1from omegaconf import OmegaConf
2from loguru import logger
3import os
4import pathlib
5import torch
6import cv2
7import numpy as np
8from scipy.spatial.transform import Rotation
9from .utils import get_3d_face_model
10from .gaze_estimator import GazeEstimator
11from .common.camera import Camera
12
13class PyGaze:
14 def __download_model__(self, target_dir):
15 logger.debug('Downloading model to {}...', target_dir)
16 output_dir = pathlib.Path(target_dir).expanduser()
17 output_dir.mkdir(exist_ok=True, parents=True)
18 output_path = os.path.join(output_dir, 'eth-xgaze_resnet18.pth')
19 if not os.path.exists(output_path):
20 logger.debug('Download the pretrained model...')
21 torch.hub.download_url_to_file(
22 'https://github.com/hysts/pytorch_mpiigaze_demo/releases/download/v0.2.2/eth-xgaze_resnet18.pth',
23 output_path)
24 else:
25 logger.debug('The pretrained model {} already exists.', output_path)
26 return output_path
27
28 def __init__(self, device="cpu", model_path = "~/.ptgaze/models"):
29
30 self.config = OmegaConf.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config/eth-xgaze.yaml'))
31 self.config.PACKAGE_ROOT = pathlib.Path(__file__).parent.resolve().as_posix()
32 self.config.device = device
33
34 self.CENTER_X = -0.01821846597470783
35 self.CENTER_Y = -0.17080051119284462
38
39 # check the model path and download the model
40 self.config.gaze_estimator.checkpoint = os.path.abspath(model_path)
41 if os.path.isfile(self.config.gaze_estimator.checkpoint):
42 logger.warning("{} is a file path but a directory is required. Removing the filename...", self.config.gaze_estimator.checkpoint)
43 self.config.gaze_estimator.checkpoint = os.path.dirname(self.config.gaze_estimator.checkpoint)
44 self.config.gaze_estimator.checkpoint = self.__download_model__(self.config.gaze_estimator.checkpoint)
45
46 # initialize
48
49 def predict(self, img):
50 results = []
51 if img is None:
52 logger.warning("Invalid image.")
53 return results
54
55 undistorted = cv2.undistort(img, self.gaze_estimator.camera.camera_matrix, self.gaze_estimator.camera.dist_coefficients)
56 faces = self.gaze_estimator.detect_faces(undistorted)
57 for face in faces:
58 self.gaze_estimator.estimate_gaze(undistorted, face)
59 return faces
60
61 def look_at_camera(self, face):
62 return face.gaze_vector[0] > self.CENTER_X - self.CENTER_THRESHOLD_X and face.gaze_vector[0] < self.CENTER_X + self.CENTER_THRESHOLD_X and face.gaze_vector[1] > self.CENTER_Y - self.CENTER_THRESHOLD_Y and face.gaze_vector[1] < self.CENTER_Y + self.CENTER_THRESHOLD_Y
63
65 def __init__(self):
68 self.AXIS_COLORS = [(0, 0, 255), (0, 255, 0), (255, 0, 0)]
69 config = OmegaConf.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config/eth-xgaze.yaml'))
70 config.PACKAGE_ROOT = pathlib.Path(__file__).parent.resolve().as_posix()
71 self.face_3d_model = get_3d_face_model(config)
72 self.camera = Camera(config.gaze_estimator.camera_params)
73
74 def render(self, img, face, draw_face_bbox=True, draw_face_landmarks=True, draw_3dface_model=True,draw_head_pose=True, draw_gaze_vector=True,
75 color = (0, 255, 0)):
76 size = 1
77
78 if draw_face_bbox:
79 bbox = np.round(face.bbox).astype(int).tolist()
80 cv2.rectangle(img, tuple(bbox[0]), tuple(bbox[1]), color, size)
81
82 if draw_face_landmarks:
83 for pt in face.landmarks:
84 pt = tuple(np.round(pt).astype(np.int).tolist())
85 cv2.circle(img, pt, size, color, cv2.FILLED)
86
87 if draw_3dface_model:
88 points2d = self.camera.project_points(face.model3d)
89 for pt in points2d:
90 pt = tuple(np.round(pt).astype(np.int).tolist())
91 cv2.circle(img, pt, size, color, cv2.FILLED)
92
93 if draw_head_pose:
94 axes3d = np.eye(3, dtype=np.float) @ Rotation.from_euler('XYZ', [0, np.pi, 0]).as_matrix()
95 axes3d = axes3d * self.head_pose_axis_length
96 axes2d = self.camera.project_points(axes3d, face.head_pose_rot.as_rotvec(), face.head_position)
97 center = face.landmarks[self.face_3d_model.NOSE_INDEX]
98 center = tuple(np.round(center).astype(np.int).tolist())
99 for pt, color in zip(axes2d, self.AXIS_COLORS):
100 pt = tuple(np.round(pt).astype(np.int).tolist())
101 cv2.line(img, center, pt, color, 2, cv2.LINE_AA)
102
103 if draw_gaze_vector:
104 start = face.center
105 end = face.center + self.gaze_visualization_length * face.gaze_vector
106 points3d = np.vstack([start, end])
107 points2d = self.camera.project_points(points3d)
108 pt0 = tuple(np.round(points2d[0]).astype(int).tolist())
109 pt1 = tuple(np.round(points2d[1]).astype(int).tolist())
110 cv2.line(img, pt0, pt1, color, 1, cv2.LINE_AA)
111
__init__(self, device="cpu", model_path="~/.ptgaze/models")
Definition pygaze.py:28
predict(self, img)
Definition pygaze.py:49
look_at_camera(self, face)
Definition pygaze.py:61
float CENTER_THRESHOLD_X
Definition pygaze.py:36
float CENTER_THRESHOLD_Y
Definition pygaze.py:37
__download_model__(self, target_dir)
Definition pygaze.py:14
render(self, img, face, draw_face_bbox=True, draw_face_landmarks=True, draw_3dface_model=True, draw_head_pose=True, draw_gaze_vector=True, color=(0, 255, 0))
Definition pygaze.py:75